/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author VuTienIT
 */
public class TinhTrangDAO {
    
    public static ArrayList<TinhTrangPOJO> getDanhSachPhieu()
    {
        ArrayList<TinhTrangPOJO> s = new ArrayList<TinhTrangPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From TinhTrangPhieu";
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                TinhTrangPOJO temp = new TinhTrangPOJO();
                temp.setMaTinhTrang(rs.getInt("MaTinhTrang"));
                temp.setTenTinhTrang(rs.getString("TenTinhTrang"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(TinhTrangPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;       
    }
    
    public static ArrayList<TinhTrangPOJO> getDanhSachDonHang()
    {
        ArrayList<TinhTrangPOJO> s = new ArrayList<TinhTrangPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = "Select * From TinhTrangDH";
            ResultSet rs =helper.executeQuery(sql);
            while(rs.next())
            {
                TinhTrangPOJO temp = new TinhTrangPOJO();
                temp.setMaTinhTrang(rs.getInt("MaTinhTrang"));
                temp.setTenTinhTrang(rs.getString("TenTinhTrang"));
                s.add(temp);
            }         
        }
        catch(SQLException ex)
        {
            Logger.getLogger(TinhTrangPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return s;
    }
    
    public static ArrayList<TinhTrangPOJO> getDanhSachDonHangCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<TinhTrangPOJO> ds = new ArrayList<TinhTrangPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_TINHTRANGDONHANG_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            TinhTrangPOJO temp = new TinhTrangPOJO();
            temp.setMaTinhTrang(rs.getInt("MaTinhTrang"));
            temp.setTenTinhTrang(rs.getString("TenTinhTrang"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
    
     public static ArrayList<TinhTrangPOJO> getDanhSachPhieuCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<TinhTrangPOJO> ds = new ArrayList<TinhTrangPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_TINHTRANGPHIEU_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            TinhTrangPOJO temp = new TinhTrangPOJO();
            temp.setMaTinhTrang(rs.getInt("MaTinhTrang"));
            temp.setTenTinhTrang(rs.getString("TenTinhTrang"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
     
     public static TinhTrangPOJO getPhieuById(int id){
       TinhTrangPOJO obj=new TinhTrangPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from tinhtrangphieu where matinhtrang="+id);
       try {
           while (rs.next()) {            
            obj.setMaTinhTrang(rs.getInt("MaTinhTrang"));
            obj.setTenTinhTrang(rs.getString("TenTinhTrang"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
     
      public static TinhTrangPOJO getDonHangById(int id){
       TinhTrangPOJO obj=new TinhTrangPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from tinhtrangdh where matinhtrang="+id);
       try {
           while (rs.next()) {            
            obj.setMaTinhTrang(rs.getInt("MaTinhTrang"));
            obj.setTenTinhTrang(rs.getString("TenTinhTrang"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
